﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common.Extensions;
using Common.Utils;

namespace Common.UI.Controls.Utils
{
	public class DateTimeChartItemsSource
	{
		public IDictionary<DateTime, Double> Items { get; private set; }

		public Double MaxValue { get; private set; }
		public DateTime MinDate { get; private set; }
		public DateTime MaxDate { get; private set; }

		public DateTimeChartItemsSource(IDictionary<DateTime, Double> items)
		{
			Items = items.OrderBy(x => x.Key).ToDictionary(x => x.Key, x => x.Value) ?? new Dictionary<DateTime, Double>();

			if (!Items.IsNullOrEmpty())
			{
				var values = Items.Values;
				var dates = Items.Keys;

				MaxValue = values.Max();
				MinDate = dates.First();
				MaxDate = dates.Last();
			}
		}

		public Double DateProportion(DateTime date, Double def = 0.0)
		{
			if (Items.IsNullOrEmpty())
				return def;

			var maxDate = (DateTimeHelper.Now - MaxDate).TotalHours < 1 ? DateTime.Now : MaxDate;
            if ((maxDate - MinDate).TotalSeconds < 1)
                return def;

			return (date - MinDate).TotalSeconds / (maxDate - MinDate).TotalSeconds;
		}

		public Double ValueProprtion(Double value, Double def)
		{
			if (Items.IsNullOrEmpty())
				return def;

			var min = 0.0;

			return (value - min) / (MaxValue - min);
		}
	}
}
